#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : micah
# @time    :  
# @function: 
# @version :

import time
import asyncio
from concurrent.futures.thread import ThreadPoolExecutor
from concurrent.futures.process import ProcessPoolExecutor

"""
concurrent.futures 统一了线程和进程的使用接口
"""


def func(value):
    time.sleep(1)
    print(value)
    return '这是当前任务的返回值'


# 线程池执行任务
# pool = ThreadPoolExecutor(max_workers=2)

# 协程中的future对象
async def async_func():
    print(1)
    await asyncio.sleep(1)
    return '这是当前协程任务的返回值..'


async def main():
    future_obj = asyncio.ensure_future((async_func()))
    res = await future_obj
    print(res)


if __name__ == '__main__':
    # 进程池执行任务
    pool = ProcessPoolExecutor(max_workers=2)

    # for i in range(4):
    #     # 当前在提交任务时会返回future对象
    #     fut = pool.submit(func, i)
    #     print(fut)
    #     # 如果想要获取到任务中的返回值可以直接就使用future对象中的result方法
    #     print(fut.result())

    asyncio.run(main())
